<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $this->load->model('luckydog/Examination_model', 'examination');
        $this->accountID = $_SESSION['adminUserInfo']['id'];
        $this->score = 60;
        $this->questionRadioNum = 1;
        $this->questionIfNum = 1;
        $this->questionCheckboxNum = 1;
        $this->questioninputNum = 1;
        $this->questionTextareaNum = 1;
        $login_status = $this->login_status = $this->check_login_status();
        if (empty($login_status)){
            redirect('/luckydog/login');
        }
        
	}
    public function response_data($data=array()){

        $this->output->set_header('Content-Type: application/json; charset=utf-8');
        $stander = array("status"=>array("code"=>200,"msg"=>"ok"),"data"=>$data);
        echo json_encode($stander);
    }
    public function index()
    {
        $_SESSION['v_menu'] = 'examination';
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $limit = 20;
        $examinationList = $this->examination->getExaminationList($per_page, $limit);
        $total_rows = $this->examination->getExaminationListCnt();
        $base_url = '/luckydog/examination/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $data = array(
            'examinationList'=>$examinationList,
            'page_links'=>$page_links,
            'per_page'=>$per_page
        );

        $this->load->view('/luckydog/examination/index',$data);
    }
    /*
     * 生成考试或补考
     * 如果是补考 则考试标题（+补考），考试部门，考试人员已定  新考试则无此项限制
     * 无论是补考或者新考 都需要重新从练习题中生成试卷
     * 补考需要从此次考试中查询出考试未达标（>60分）的员工列表 供重考时候默认勾选
     */
    public function toSetExam(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $examinationInfo = array();
        $userMsg = "部门全部员工";
        if($id){
            $examinationInfo = $this->examination->getExaminationInfo($id);
            if(!empty($examinationInfo)){
                $examinationInfo = $examinationInfo[0];
                $examinationInfo['examinationTitle'] = $examinationInfo['examinationTitle']."(补考)";
                $userMsg = "挂科全部员工";
            }
        }
        $deptList = $this->examination->get_cat_list();
        $data = array(
            'examinationInfo'=>$examinationInfo,
            'deptList'=>$deptList,
            'userMsg'=>$userMsg
        );
        $this->load->view('/luckydog/examination/set_exam',$data);
    }

    /**
     * 创建考试 生成试卷
     * step 1 生成考试信息 list
     * step 2 随机查询练习题拼接试卷 paper question
     * step 3 补考查询考试未达标人员 插入json paper
     * step 4 随机将生成试卷安排不同人员参加考试 paper
     */
    public function edit_exam()
    {
        $examID = $this->input->post("examID",true) ? $this->input->post("examID",true) : 0; //存在ID 则为补考
        $examinationName = $this->input->post('examinationName', TRUE);
        $dateTime = $this->input->post('dateTime', TRUE);
        $deptID = $this->input->post('deptID',TRUE);
        $radioNum = $this->input->post('radioNum',TRUE);
        $checkNum = $this->input->post('checkNum',TRUE);
        $ifNum = $this->input->post('ifNum',TRUE);
        $inputNum = $this->input->post('inputNum',TRUE);
        $textareaNum = $this->input->post('textareaNum',TRUE);

        /*
         * 数据未补充前 每类型最多可创建一道题
         */
        $radioNum = $this->questionRadioNum;
        $checkNum = $this->questionCheckboxNum;
        $ifNum = $this->questionIfNum;
        $inputNum = $this->questioninputNum;
        $textareaNum = $this->questionTextareaNum;



        $tableExam = 'tr_examination_list';
        $dateTime = date("Y-m-d",strtotime($dateTime));
        $paramsData = array(
            'examinationTitle'=>$examinationName,
            'examinationTime'=>$dateTime,
            'userID'=>$this->accountID,
            'examinationDept'=>$deptID
        );
        $insertExamID = $this->examination->insertData($tableExam,$paramsData);
        //查询部门下所有员工
        $userInfo = $this->examination->searchUserAll($deptID);
        if($examID>0){
            $userInfo = $this->examination->searchUserLower($examID,$this->score);
        }
        shuffle($userInfo);  //将数组打乱
        $examNum = $this->examination->getRouterNum($userInfo);  //补考 新考都需要
        if(!empty($examNum)){
            $tablePaper = 'tr_examination_paper';
            $paramsData['paperTitle'] = $examinationName;
            $paramsData['examinationID'] = $insertExamID;
            unset($paramsData['examinationTitle']);
            unset($paramsData['examinationTime']);
            foreach($examNum as $key=>$val){
                $paramsData['examinationCustomer'] = json_encode($val);
                $insertPaperID = $this->examination->insertData($tablePaper,$paramsData);
                //next 查询随机练习题插入question中即OK  radio checkbox if input textarea
                $questionNewInfo = array("1"=>$radioNum,"2"=>$checkNum,"3"=>$ifNum,"4"=>$inputNum,"5"=>$textareaNum);
                $pjQuestion = $this->examination->pjQuestion($insertExamID,$insertPaperID,$questionNewInfo,$deptID);
                /*
                 * 发送考试邮件
                 */
                $username = $this->examination->getUserInfo($val);
                $paramsEmailData=array(
                    "messageType"=>2,
                    "subject"=>"参加".$examinationName,
                    "content"=>"请".$dateTime."日参加<a href='".BASEURL."examination/show?type=2&paperID=".$insertPaperID."&extID=".$insertExamID."'></a>",
                    "usernames"=>implode(",",$username),
                    "tosendtype"=>"usernames"
                );
                $sendMail = $this->send_email($paramsEmailData);
            }
        }

        $this->response_data();

    }
    /*
     * 取消考试
     * 删除考试列表  发送取消邮件
     */
    public function cancel(){
        $id = $this->input->post('id', TRUE);
        $cancel = $this->examination->cancel($id);
        /*
         * 发送邮件
         */
        $account = isset($cancel['examinationCustomer']) ? $cancel['examinationCustomer'] : array();
        $title = isset($cancel['paperTitle']) ? $cancel['paperTitle'][0] : array();
        $username = $this->examination->getUserInfo($account);
        $paramsEmailData=array(
            "messageType"=>2,
            "subject"=>$title."已取消",
            "content"=>$title."已取消,请知晓",
            "usernames"=>implode(",",$username),
            "tosendtype"=>"usernames"
        );
        $sendMail = $this->send_email($paramsEmailData);
        $this->response_data();
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/role.php */